const toString = Object.prototype.toString


// is Date 类型保护
export function isDate(val: any): val is Date{
    return toString.call(val) === '[object Date]'
}


// is object
// export function isObject(val:any): val is Object{
//     return val !==null && typeof val === 'object'
// }

// 是否是普通对象
export function isPlainObject(val:any): val is Object{
    // 注意  必须是单引号
    return toString.call(val) === '[object Object]'
}

// 上传 如果是上传  删除 自定义 header type 由浏览器自动设置
export function isFormData(val: any): boolean {
    return typeof val !== 'undefined' && val instanceof FormData
}

export function extend<T, U>(to: T, from: U): T & U {
    for (const key in from) {
      ;(to as T & U)[key] = from[key] as any
    }
    return to as T & U
}


export function isURLSearchParams(val: any): val is URLSearchParams {
    return typeof val !== 'undefined' && val instanceof URLSearchParams
}

// 深拷贝 对象副本
export function deepMerge(...objs: any[]): any {
    const result = Object.create(null)
  
    objs.forEach(obj => {
      if (obj) {
        Object.keys(obj).forEach(key => {
          const val = obj[key]
          if (isPlainObject(val)) {
            if (isPlainObject(result[key])) {
              result[key] = deepMerge(result[key], val)
            } else {
              result[key] = deepMerge({}, val)
            }
          } else {
            result[key] = val
          }
        })
      }
    })
  
    return result
}


